<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: liugohua@clcw.com.cn  Blog:http://blog.sina.com.cn/saverneo    |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\CarDealer;

/**
 * 车商列表
 * @author       Eric Lee
 */
class DealerList
{

    /**
     * 获取车商列表
     * @param integer $page
     * @param integer $size
     * @param array $paramer
     * @return multitype:array(list,total)
     */
    public function getDealers($page = 1, $size = 10, $paramer)
    {
        $map = array();
        $map['cd.category'] = array('eq', 1);
        $where = array();
        //类型
        if ($paramer['keyword']) {
            $where['u.user_name'] = array('like', '%' . $paramer['keyword'] . '%');
            $where['cd.fullname'] = array('like', '%' . $paramer['keyword'] . '%');
            $where['cd.contact_person'] = array('like', '%' . $paramer['keyword'] . '%');
            $where['_logic'] = 'OR';
            $map['_complex'] = $where;
        }

        $co = M('car_dealer');
        $prefix = C('DB_PREFIX');
        $total = $co->alias('cd')->join($prefix . "dealer_user du on du.dealer_id=cd.dealer_id")
            ->join($prefix . "user u on du.uid=u.uid")->where($map)->count();
        $list = $co->alias('cd')->field('cd.dealer_id,cd.contact_person,cd.fullname,u.user_name')
            ->join($prefix . "dealer_user du on du.dealer_id=cd.dealer_id")
            ->join($prefix . "user u on du.uid=u.uid")
            ->where($map)->limit($size)->page($page)->order("dealer_id desc")->select();
        //echo $co->getLastSql();
        foreach ($list as $key => $val) {
        }
        //echo M()->getLastSql();
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取车商代金券列表
     * @param integer $page
     * @param integer $size
     * @param array $paramer
     * @return multitype:array(list,total)
     */
    public function getDealerCoupons($page = 1, $size = 10, $paramer)
    {
        $map = array();
        //类型
        $map['dc.dealer_id'] = $paramer['dealer_id'];
        switch ($paramer['curr_tab']) {
            case '0'://未使用
                $where = array();
                $where['dc.use_status'] = array('eq', '0');
                $where['c.end_date'] = array('egt', date('Y-m-d'));
                $map['_complex'] = $where;
                break;
            case '1'://已使用
                $map['dc.use_status'] = array('eq', '1');
                break;
            case '2'://已过期
                $map['dc.use_status'] = array('neq', '1');
                $map['c.end_date'] = array('lt', date('Y-m-d'));
                break;
            default:
                $where = array();
                $where['c.use_status'] = array('eq', '0');
                $where['c.end_date'] = array('egt', date('Y-m-d'));
                $map['_complex'] = $where;
                break;
        }
        if ($paramer['coupon_type_id']) {
            $map['c.coupon_type_id'] = array('eq', $paramer['coupon_type_id']);
        }
        if ($paramer['get_time']) {
            $map['dc.createtime'] = array('like', date('Y-m-d', strtotime($paramer['get_time'])) . '%');
        }
        if ($paramer['end_date']) {
            $map['c.end_date'] = array('like', date('Y-m-d', strtotime($paramer['end_date'])) . '%');
        }
        $prefix = C('DB_PREFIX');
        $co = M('dealer_coupon');
        $total = $co->alias('dc')
            ->join($prefix . "coupon c ON c.coupon_id=dc.coupon_id")
            ->join($prefix . "coupon_type ct on ct.coupon_type_id=c.coupon_type_id")
            ->where($map)->count();

        $cols = 'c.coupon_no,ct.amount,ct.type_name,dc.createtime,c.start_date,c.end_date';
        $list = $co->alias('dc')->field($cols)
            ->join($prefix . "coupon c ON c.coupon_id=dc.coupon_id")
            ->join(C('DB_PREFIX') . "coupon_type ct on ct.coupon_type_id=c.coupon_type_id")
            ->where($map)->limit($size)->page($page)->order("dc.dc_id")->select();
        //echo $co->getLastSql();
        foreach ($list as $key => $val) {
        }
        //echo M()->getLastSql();
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }
}

?>